<?php
class Marketing_AjaxController extends Zendvn_Controller_Action {
		
	protected $_arrParam; //Mang tham so nhan duoc khi mot Action chay
	protected $_currentController; //Duong dan cua Controller
	protected $_actionMain; //Duong dan cua Action chinh
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 15,
									'pageRange' => 10,
									'currentPage' => 1
							);
	protected $_namespace;
	
	public function init(){
		
		$this->_arrParam = $this->_request->getParams(); //Mang tham so nhan duoc khi mot Action chay
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller']; //Duong dan cua Controller
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index'; //Duong dan cua Action chinh
		
		//Luu cac du lieu filter vaof SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		//Lay thong tin so phan tu tren mot trang
		if(isset($this->_arrParam['limitPage'])){
			$ssFilter->limitPage = $this->_request->getParam('limitPage');
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}elseif(!empty($ssFilter->limitPage)){
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}
		
		//Trang hien tai
		if(isset($this->_arrParam['page'])){
			$this->_paginator['currentPage'] = $this->_arrParam['page'];
			$this->_page = '/page/' . $this->_arrParam['page'];
		}
		
		//Truyen thong tin phan trang vao mang du lieu
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$this->view->siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/public/" . $this->view->siteConfig['template']['site'];
		$this->loadTemplate($template_path, 'template.ini', 'user');
		
		$language = Zend_Registry::get('language');
		$this->view->language = $language['language'];
	}
	
	public function listMarketingAction() {
		$this->_helper->layout->disableLayout();
		
		$tblCategory = new Marketing_Model_Category();
		$this->view->slbCategory	= $tblCategory->itemInSelectbox();
	}
	
	public function loadMarketingAction() {
		$this->_arrParam['paginator']['itemCountPerPage'] = 12;
		$this->_paginator['itemCountPerPage'] = 12;
		
		$this->_helper->layout->disableLayout();
		
		$tblMarketing = new Marketing_Model_Item();
		$this->view->Marketing 	= $tblMarketing->listItem($this->_arrParam, array('task' => 'admin-list'));
		
		$totalItem = $tblMarketing->countItem($this->_arrParam, array('task' => 'admin-list'));
		$paginator = new Zendvn_Paginator();
		$this->view->paginator = $paginator->createPaginator($totalItem, $this->_paginator);
	}
	
	public function xoaXmlAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		if (!empty($this->_arrParam['id'])) {
			$id = $this->_arrParam['id'];
			$filename 	= MODULE_PATH . '/marketing/xml/'.$this->_arrParam['file_name'].'.xml';
			$q 	= "//*[id ='" . $id . "']";

			$xml = simplexml_load_file($filename);
			$target = $xml->xpath($q);
				
			if(!$target)
				return; //Returns null
				
			$domRef = dom_import_simplexml($target[0]); //Select position 0 in XPath array
			$domRef->parentNode->removeChild($domRef);
				
			$dom = new DOMDocument('1.0');
			$dom->preserveWhiteSpace = false;
			$dom->formatOutput = true;
			$dom->loadXML($xml->asXML());
			$dom->save($filename);
		}
	}
	
	public function trangthaiXmlAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		if (!empty($this->_arrParam['id'])) {
			$id = $this->_arrParam['id'];
			$filename 	= MODULE_PATH . '/marketing/xml/'.$this->_arrParam['file_name'].'.xml';
				
			$xml = simplexml_load_file($filename);
				
			$item = $xml->xpath("//*[id ='" . $id . "']");
			$item[0]->trangthai = $this->_arrParam['trangthai'];
			$xml->asXML($filename);
		}
	}
	
	public function guiEmailAction() {
		
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$siteConfig = Zend_Registry::get('siteConfig');
		
		$options = $siteConfig['config_mail'];
		if($options['status'] == 1){
			$options['smtpsecure'] = $siteConfig['config_mail']['smtpsecure'];
			$options['smtphost'] = $siteConfig['config_mail']['smtphost'];
			$options['smtpport'] = $siteConfig['config_mail']['smtpport'];
			$options['smtpuser'] = $siteConfig['config_mail']['smtpuser'];
			$options['smtppass'] = $siteConfig['config_mail']['smtppass'];
			$options['mailfrom'] = $siteConfig['config_mail']['mailfrom'];
			$options['fromname'] = $siteConfig['config_mail']['fromname'];
			$options['tomail'] = $this->_arrParam['email'];//Mail người nhận
			$options['title'] = $siteConfig['config_mail']['fromname']; //Tên người gửi
			$options['subject'] = $this->_arrParam['title'];//Tiêu đề thư
		
			$content = htmlspecialchars_decode($this->_arrParam['content']);
		
			$mail = new Zendvn_Phpmailer();
			$mail->send($options,$content);
			
			if(!empty($this->_arrParam['file_name'])) {
				if (!empty($this->_arrParam['id'])) {
					$id = $this->_arrParam['id'];
					$filename 	= MODULE_PATH . '/marketing/xml/'.$this->_arrParam['file_name'].'.xml';
						
					$xml = simplexml_load_file($filename);
						
					$item = $xml->xpath("//*[id ='" . $id . "']");
					$item[0]->trangthai_email = 1;
					$xml->asXML($filename);
				}
			}
		}
		//echo $this->_arrParam['file_name'] . ' - ' . $this->_arrParam['id'] . ' - ' . $this->_arrParam['email'] . ' - ' . $this->_arrParam['name'];
	}
		
}
